package tsing.javase.stream;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private String name;
    private int score;
    private String gender;






    public static void main(String[] args) {
        List<Student> students = Arrays.asList(
                new Student("张三", 60, "男"),
                new Student("李四", 80, "女"),
                new Student("王五", 50, "男"),
                new Student("赵六", 70, "女"),
                new Student("陈七", 30, "女")
        );
/*        Map<String, List<Student>> failedStudentsByGender = students.stream()
                .filter(student -> student.getScore() < 60).collect(Collectors.groupingBy(Student::getGender));
        System.out.println(failedStudentsByGender);*/

/*        Map<String, List<Student>> map = students.stream().collect(Collectors.groupingBy(Student::getGender));

        Set<Map.Entry<String, List<Student>>> entries = map.entrySet();
        for (Map.Entry<String, List<Student>> entry : entries) {

            entry.getValue().stream().filter(e-> Objects.equals(e.getName(),"张三")).findFirst().ifPresent(e->e.setScore(e.getScore()+10));

        }

        System.out.println(JSON.toJSONString(map));

        System.out.println("--------------------------");
        System.out.println(students);*/

 /*       // Use a Set to remove duplicates
        Set<Student> uniquePeople = new HashSet<>(students);

        // Convert back to List if needed
        List<Student> resultList = new ArrayList<>(uniquePeople);
        System.out.println(JSON.toJSONString(resultList));*/


/*        List<String> collect = students.stream().map(Student::getName).collect(Collectors.toList());
        StringBuilder sb = new StringBuilder();

        collect.forEach(e->{
            sb.append(e).append("|");
        });


        System.out.println(sb.substring(0,sb.length()-1));
        System.out.println(sb.delete(0,sb.length()));*/

/*
        String str = "a|b|c";
        Set<String> linkIdSet = new HashSet<>(Arrays.asList(str.split("\\|")));

        System.out.println(JSON.toJSONString(linkIdSet));*/

 /*       StringBuilder sb = new StringBuilder("a,b,c,");

        System.out.println(sb.substring(0,sb.length()-1));*/



//        String a = "0,100";
//        String a = "0,100,200,226";
/*        String a = "0,10,200";


        System.out.println(getParentDeptId(a));*/


        System.out.println(new BigDecimal(2).setScale(2,RoundingMode.HALF_UP));


        Set<String> typeSet = new HashSet<>();
        typeSet.add("a");
        typeSet.add("b");
        typeSet.add("c");
        typeSet.add("d");
        System.out.println(String.join(",",typeSet));


    }


    public static String getParentDeptId(String ancestorsId){
        String result = null;
        String targetString = "0,100";
        try {
            if(ancestorsId.startsWith(targetString)){
                if(!ancestorsId.equals(targetString)){
                    // 找到目标字符串的起始位置
                    int startIndex = ancestorsId.indexOf(targetString);

                    if (startIndex != -1) {
                        // 计算目标字符串后的第一个字符的位置
                        int endIndex = startIndex + targetString.length()+1;
                        // 提取目标字符串后的子字符串
                        String resultString = ancestorsId.substring(endIndex);

                        if(StrUtil.isNotBlank(resultString)){
                            result = resultString.split(",")[0];
                        }
                    }

                }
            }
        } catch (Exception e) {
            System.out.println("部门层级："+ancestorsId+"获取父级部门id出错，原因："+e);
        }
        return  result;
    }
}
